<?php

/**
 * Created by PhpStorm.
 * User: bob
 * Date: 21/9/25
 * Time: 上午10:37
 */

namespace App\Api\Admin\Controllers;

use App\Api\Account\Services\BusinessService;
use App\Api\Admin\Models\Tag;
use App\Api\Web\Models\House;
use App\Api\Web\Models\HouseTag;
use App\Api\Web\Models\HouseWanted;
use App\Api\Web\Models\OrderHouse;
use App\Api\Web\Services\HouseService;
use App\Http\Controllers\Controller;
use App\User;
use Carbon\Carbon;
use function GuzzleHttp\Promise\promise_for;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function orderIndex(Request $request)
    {
        $params = $request->all();
        $status = data_get($params, 'status', '');
        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);

//        if (empty($role)) {
//            throw new \LogicException('缺少必要参数', 100);
//        }

        $m = OrderHouse::leftJoin('users as user', 'user.id', '=', 'order_house.tenement_user_id')
            ->leftJoin('users as landlord', 'landlord.id', '=', 'order_house.landlord_user_id')
            ->where('order_house.status', '>', -900);


        if (!empty($status)) {
            $m = $m->where('order_house.status', '=', $status);
        }

        $m = $m->orderBy('order_house.id', 'desc')
            ->select('order_house.*', 'user.name as tenement_name', 'user.head_img as tenement_head_img', 'landlord.name as landlord_name', 'landlord.head_img as landlord_head_img' )
            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        foreach ($m['data'] as $k => $v) {
            $m['data'][$k]['contract_name'] = data_get(OrderHouse::$contract, data_get($v, 'contract', ''), '未知');
            $m['data'][$k]['payment_method_name'] = data_get(OrderHouse::$payment_method, data_get($v, 'payment_method', ''), '未知');
            $m['data'][$k]['status_name'] = data_get(OrderHouse::$status, data_get($v, 'status', ''), '未知');
            $m['data'][$k]['tenement_status_name'] = data_get(OrderHouse::$tenement_status, data_get($v, 'tenement_status', ''), '未知');
            $m['data'][$k]['landlord_status_name'] = data_get(OrderHouse::$landlord_status, data_get($v, 'landlord_status', ''), '未知');
            $m['data'][$k]['photos'] = json_decode(data_get($v, 'photos', ''));
        }

        return $this->formatOutput($m);
    }

    public function orderUpdate(Request $request)
    {
        $params = $request->all();
        $status = data_get($params, 'status', '');
        $id = data_get($params, 'id', '');
        if (empty($id)) {
            throw new \LogicException('缺少必要参数', 100);
        }

        $updates = [];

        empty($status) ? : $updates['status'] = $status ;

        if (!empty($updates)) {
            OrderHouse::find($id)
                ->update($updates);
        }

        return $this->formatOutput('success');
    }
    /**
     * 划拨
     * @param Request $request
     * @return array
     */
    public function transfer(Request $request)
    {
        $params = $request->all();
        $id = data_get($params, 'id', '');
        $action = data_get($params, 'action', '');
        if (empty($id) || empty($action)) {
            throw new \LogicException('缺少必要参数', 100);
        }
        $m = OrderHouse::find($id);
        $mm = clone $m;
        $ding_total = data_get($m, 'ding_total', '');
        $pay_total = data_get($m, 'pay_total', '');
        $ding_integral = data_get($m, 'ding_integral', '');
        switch ($action) {
            case 'tenement':
                $m->update([
                    'status' => '-200',
                    'tenement_status' => '-200',
                    'landlord_status' => '-200',
                ]);
                BusinessService::commonTransaction(data_get($mm, 'tenement_user_id', ''), $pay_total, data_get($mm, 'id', ''), 'order_house', data_get($mm, 'house_titile', '') . '取消退订金');
                BusinessService::commonIntegralTransaction(data_get($mm, 'tenement_user_id', ''), $ding_integral, data_get($mm, 'id', ''), 'order_house', data_get($mm, 'house_titile', '') . '取消退积分');
                BusinessService::freezeTransaction(data_get($mm, 'landlord_user_id', 0), -$ding_total, data_get($mm, 'id', ''), 'order_house', data_get($mm, 'house_titile', '') . '取消退订金');
                break;
            case 'landlord':
                $m->update([
                    'status' => '-200',
                    'tenement_status' => '-200',
                    'landlord_status' => '-200',
                ]);
                BusinessService::freezeTransaction(data_get($mm, 'landlord_user_id', 0), -$pay_total, data_get($mm, 'id', ''), 'order_house', data_get($mm, 'house_titile', '').'定金解冻');
                BusinessService::commonTransaction(data_get($mm, 'landlord_user_id', 0), $pay_total, data_get($mm, 'id', ''), 'order_house', data_get($mm, 'house_titile', '').'定金解冻');
                break;
            case 'admin':
                $m->update([
                    'status' => '-200',
                    'tenement_status' => '-200',
                    'landlord_status' => '-200',
                ]);
                BusinessService::freezeTransaction(data_get($mm, 'landlord_user_id', 0), -$pay_total, data_get($mm, 'id', ''), 'order_house', data_get($mm, 'house_titile', '').'取消退订金');
                break;
            default:
                throw new \LogicException('缺少必要参数', 110);
        }


        return $this->formatOutput('success');


    }

}
